package com.whz.workmanageol.controller;

import com.whz.workmanageol.entity.User;
import com.whz.workmanageol.entity.Work;
import com.whz.workmanageol.service.WorkService;
import com.whz.workmanageol.utils.FileUpload;
import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/pageoffice")
public class PageOfficeController {

    @Autowired
    private WorkService workService;

    @GetMapping(value = "/index")
    public String showIndex(){
        return "pageoffice/Index";
    }

    @GetMapping(value = "readWord")
    public String readWord(HttpServletRequest request, Model model){
        String workid = request.getParameter("workid");
        Work theWork = workService.findWorkById(workid);
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage("/poserver.zz");//设置授权程序servlet
        poCtrl.addCustomToolButton("保存","Save",1); //添加自定义按钮
        poCtrl.setSaveFilePage("/pageoffice/save");//设置保存的action
        // 学生作业所在的路径
        String destFile = FileUpload.FILE_UPLOAD_PATH + theWork.getFilepath() + theWork.getFilename() + theWork.getSuffix();
        poCtrl.webOpen(destFile, OpenModeType.docAdmin, "");
        model.addAttribute("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        String role = ((User)request.getSession().getAttribute("user")).getRole();
        if("teacher".equals(role)){
            // 若当前用户角色为教师 设置批阅时间
            workService.readMark(workid);
        }

        return "pageoffice/Word";
    }

    @RequestMapping(value = "/save")
    public void saveFile(HttpServletRequest request, HttpServletResponse response){
        FileSaver fs = new FileSaver(request, response);
        fs.close();
    }
}
